package lg.minecraft.plugin.testeconomy.command;

import lg.minecraft.plugin.testeconomy.TestEconomy;
import lg.minecraft.plugin.testeconomy.config.MessageConfig;
import lg.minecraft.plugin.testeconomy.storage.BaseStorage;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 * @author langle__
 * @version 1.0
 */
public class SetCommand extends SubCommand {

    @Override
    public String getName() {
        return "set";
    }

    @Override
    public void onCommand(CommandSender sender, String[] args) {
        if (args.length <= 1) {
            sender.sendMessage(MessageConfig.getSET_USAGE());
            return;
        }
        Player player = argToPlayer(args[0]);
        if (player == null) {
            sender.sendMessage(MessageConfig.getPLAYER_NOT_FOUND());
            return;
        }
        Double amount = argToDouble(args[1]);
        if (amount == null) {
            sender.sendMessage(MessageConfig.getINVALID_AMOUNT());
            return;
        }
        BaseStorage storage = TestEconomy.getStorage();
        double balance = storage.getBalance(player);
        storage.withdrawPlayer(player, balance);
        storage.depositPlayer(player, amount);
        double newBalance = storage.getBalance(player);
        sender.sendMessage(MessageConfig.getSetSuccess(player.getName(), newBalance));
    }

}
